package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @Reference
    private UserService userService;

    @RequestMapping("/getMenu")
    public Result getMenu() {
        try {
            //获取当前登录的用户信息
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            com.itheima.pojo.User user1 = userService.findByUsername(user.getUsername());

            Set<Role> roles = user1.getRoles();
            List<Map<String, Object>> list = null;
            for (Role role : roles) {
                System.out.println(role.getId());
               list = menuService.getMenu(role.getId());
            }

            return new Result(true, MessageConstant.GET_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

}
